<?php
//class MemberController extends UserBaseC{
class MemberController extends UserLoginBaseC{
	//收藏列表
	public function actionCollect(){
        $datas = MemberCollectModel::getUser(yii::app()->user->id);
        $arrResult['count'] = count($datas);
        $intPageSize = 30;
        $intPage = empty($_GET['page']) ? 1 : $_GET['page'];
        if($arrResult['count'] > $intPageSize){
            $objPager = new CPagination($arrResult['count']);
            $objPager->setCurrentPage($intPage - 1 < 0 ? 0 : $intPage - 1);
            $objPager->pageSize = $intPageSize;
            $arrResult['page'] = $objPager; 
            //限定取个数
            $count = 0;
            $begin = $objPager->getOffset();
            foreach ($datas as $key => $data) {
                $count++;
                if($count <= $begin){
                    continue;
                }else{
                    if($count > ($begin+$intPageSize)){
                        break;
                    }
                    $arrResult['data'][$key] = $data;
                }
            }            
        }else{
            $arrResult['data'] = $datas;
        }
        empty($arrResult['data']) ? $arrResult['data'] = array() : '';
        $this->assign('data', $arrResult);
        $this->render('collect');  
	}
    //消息列表
	public function actionMessage(){
        $datas = MessageModel::getListUser(yii::app()->user->id);
        $arrResult['count'] = count($datas);
        $intPageSize = 30;
        $intPage = empty($_GET['page']) ? 1 : $_GET['page'];
        if($arrResult['count'] > $intPageSize){
            $objPager = new CPagination($arrResult['count']);
            $objPager->setCurrentPage($intPage - 1 < 0 ? 0 : $intPage - 1);
            $objPager->pageSize = $intPageSize;
            $arrResult['page'] = $objPager; 
            //限定取个数
            $count = 0;
            $begin = $objPager->getOffset();
            foreach ($datas as $key => $data) {
                $count++;
                if($count <= $begin){
                    continue;
                }else{
                    if($count > ($begin+$intPageSize)){
                        break;
                    }
                    $arrResult['data'][$key] = $data;
                }
            }            
        }else{
            $arrResult['data'] = $datas;
        }
        empty($arrResult['data']) ? $arrResult['data'] = array() : '';
        $this->assign('data', $arrResult);
        $this->render('message');  
	}
    //修改与删除消息 
	public function actionUpdateMessage(){
        $data = $_POST['id'];
        if(empty($data) || !is_array($data)){
            $this->error('提交错误');
        }
        if($_POST['type'] == 1){
            MessageModel::deleteList($data);
        }else{
            MessageModel::updateList($data);
        }
        $this->success('处理成功');
	}

    //
	public function actionIndex(){
		$this->actionCollect();
	}
	/**
	 * 批量删除收藏
	 * @return [type] [description]
	 */
	public function actionDeleteList(){
		$data = $_POST['id'];
		if(empty($data) || !is_array($data)){
			$this->error('提交错误');
		}
		if(MemberCollectModel::deleteList($data, yii::app()->user->id)){
			$this->success('处理成功');
		}else{
			$this->error('处理失败');
		}
	}

    /**
     * 修改资料,包括修改密码/
     * @return [type] [description]
     */
    public function actionUpdate(){
        $objModel = $this->loadModel(yii::app()->user->id);

        if(IS_POST)
        {
            //密码验证
            $_POST['Password'] = empty($_POST['Password']) ? '' : $_POST['Password'];
            $_POST['newPassword'] = empty($_POST['newPassword']) ? '' : $_POST['newPassword'];  
            $_POST['newPassword1'] = empty($_POST['newPassword1']) ? '' : $_POST['newPassword1'];  
            if(!$objModel->updatePassword($_POST['Password'], $_POST['newPassword'], $_POST['newPassword1'])){
                $this->error($objModel->getOneError());             
            }
            unset($_POST['Password']);
            if($_POST['newPassword']){
                $objModel->Password = $_POST['newPassword'];
            }
            $objModel->attributes = $_POST;
            //修改资料
            if($objModel->save())
            {           
                $this->success('成功修改');
            }else
            {
                $error = $objModel->getOneError();
                $this->error($error ? $error : '修改失败！');                
            }
        }
        $this->render('update',array('model' => $objModel));        
    }

    public function loadModel($id)
    {
        $model=MemberModel::model()->findByPk($id);
        if($model===null)
            $this->error('找不到记录');
        return $model;
    }

}